<?php

declare(strict_types=1);

namespace app\model;

use think\cache\Driver;
use think\facade\Db;
use think\Model;

/**
 * @mixin \think\Model
 */
class Column extends Model
{
    //获取单一数据
    public static function find($id)
    {
        $find = Db::name('column')->where('id', $id)->where('del', '0')->find();
        return $find;
    }
    //栏目列表
    public static function list($where = "")
    {
        $list = Db::name('column')->where($where)->where('del', '0')->order('px')->select();
        return $list;
    }
    //栏目添加
    public static function add($data)
    {
        $data['add_date'] = time();
        $res = Db::name('column')->insert($data);
        return $res;
    }
    //栏目编辑
    public static function edit($data)
    {
        $data['rep_date'] = time();
        $res = Db::name('column')->update($data);
        return $res;
    }
    //栏目删除
    public static function del($id)
    {
        $son = Db::name('column')->where('parent_id', $id)->where('del', '0')->find();
        if ($son) {
            return '已有下级，请先删除下级';
        } else {
            $res = Db::name('column')->where('id', $id)->update(['del' => '1', 'rep_date' => time()]);
            if ($res) {
                return 'success';
            } else {
                return '操作失败';
            }
        }
    }
}
